import WorkerBase
import MSData
import MSLogger
import MSDBTemplates

import kinterbasdb; kinterbasdb.init(type_conv=200)
import time


class Worker(WorkerBase.WorkerBase):
    def __init__(self, Path, User, Pass):
        self.DBPath = Path
        self.DBUser = User
        self.DBPass = Pass
        self.connection = None

    def getConnectionName(self):
        return '(FB:%s)' % (str(self.DBPath))

    def getConnection(self):
        return kinterbasdb.connect(dsn=self.DBPath, user=self.DBUser, password=self.DBPass)
    
    def isConnectionGood(self):
        return False
        if self.connection and not self.connection.closed:
            return True
        return False

    def writeEventsStatic(self, MSObjects):
        self.connect()
        cursor  = self.connection.cursor()
        recId   = MSDBTemplates.GET_LastRecId_FROM_sgn(cursor)
        for obj in MSObjects:
            try:
                doneNum         = 0
                obj.canClearPGSMEvents  = True
                obj.eventsPGSM.sort()
                for e in obj.eventsPGSM:
                    try:
                        e.userWriterId = str(self.DBUser)
                        recId = MSDBTemplates.STORE_MSEvent_TO_sgn(cursor, e, recId)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: object(%s) PGSM event processing failed: %s' % (str(obj.objectPCNId), str(err)))
                        obj.canClearPGSMEvents = False
                        continue
                if len(obj.eventsPGSM) > 0:
                    MSLogger.logInfo('Worker::writeEventsStatic: object(%s) PGSM done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsPGSM)))
                else:
                    MSLogger.logDebug('Worker::writeEventsStatic: object(%s) PGSM done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsPGSM)))


                doneNum         = 0
                obj.canClearContactIdEvents  = True
                obj.eventsContactId.sort()
                for e in obj.eventsContactId:
                    try:
                        e.userWriterId = str(self.DBUser)
                        recId = MSDBTemplates.STORE_MSEvent_TO_sgn(cursor, e, recId)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: object(%s) ContactId event processing failed: %s' % (str(obj.objectPCNId), str(err)))
                        obj.canClearContactIdEvents = False
                        continue
                if len(obj.eventsContactId) > 0:
                    MSLogger.logInfo('Worker::writeEventsStatic: object(%s) ContactId done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsContactId)))
                else:
                    MSLogger.logDebug('Worker::writeEventsStatic: object(%s) ContactId done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsContactId)))

                doneNum         = 0
                obj.canClearBolidEvents  = True
                obj.eventsBolid.sort()
                for e in obj.eventsBolid:
                    try:
                        e.userWriterId = str(self.DBUser)
                        recId = MSDBTemplates.STORE_MSEvent_TO_sgn(cursor, e, recId)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: object(%s) Bolid event processing failed: %s' % (str(obj.objectPCNId), str(err)))
                        self.canClearBolidEvents = False
                        continue
                if len(obj.eventsBolid):
                    MSLogger.logInfo('Worker::writeEventsStatic: object(%s) Bolid done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsBolid)))
                else:
                    MSLogger.logDebug('Worker::writeEventsStatic: object(%s) Bolid done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsBolid)))

            except Exception, err:
                MSLogger.logError('Worker::writeEventsStatic: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        self.connection.commit()
        return
